﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyToHelp.Business;
using EasyToHelp.Entity;
using EasyToHelp.Service;
using Microsoft.AspNetCore.Mvc;

namespace EasyToHelp.WAP.Controllers
{
    public class ReceiveaddressController : BaseController
    {
        IReceiveAdressService receiveAdress;
        public ReceiveaddressController(IReceiveAdressService _receiveAdress)
        {
            receiveAdress = _receiveAdress;
        }


        #region 收货地址查看
        /// <summary>
        /// 收货地址查看
        /// </summary>
        /// <returns></returns>
        public IActionResult MyAddress()
        {
           
            ViewBag.List = receiveAdress.GetList(UserId().ToString());
            return View();
        }

        #endregion

        #region 新增
        /// <summary>
        /// 新增收货地址
        /// </summary>
        /// <returns></returns>
      
        public IActionResult AddressData(ReceiveAddress model)
        {
            if (string.IsNullOrWhiteSpace(model.ReNames))
            {
                return Json(ResultInfo.Error("请输入姓名！"));
            }
            if (string.IsNullOrWhiteSpace(model.Rephone))
            {
                return Json(ResultInfo.Error("请输入手机号！"));
            }
            if (string.IsNullOrWhiteSpace(model.detailaddres))
            {
                return Json(ResultInfo.Error("请输入详细地址"));
            }
            if (string.IsNullOrWhiteSpace(model.province))
            {
                return Json(ResultInfo.Error("请选择地址！"));
            }
            string[] DZ = model.province.Split(' ');
            model.province = DZ[0].ToString();
            model.citys = DZ[1].ToString();
            model.areas = DZ[2].ToString();
            model.UserId = UserId().ToString();
            if (receiveAdress.Add(model) > 0)
            {
                return Json(ResultInfo.Success("提交成功！"));
            }
            return Json(ResultInfo.Error("操作失败！"));
        }
        #endregion 新增

        #region 更新
        public IActionResult UpDateAddress(int ID)
        {
            ReceiveAddress model = new ReceiveAddress();
            if (ID > 0)
                model = receiveAdress.GetModel(ID);
            return View(model);

        }

        public IActionResult UpdataAddress(ReceiveAddress model)
        {
            if (string.IsNullOrWhiteSpace(model.ReNames))
            {
                return Json(ResultInfo.Error("请输入姓名！"));
            }
            if (string.IsNullOrWhiteSpace(model.Rephone))
            {
                return Json(ResultInfo.Error("请输入手机号！"));
            }
            if (string.IsNullOrWhiteSpace(model.detailaddres))
            {
                return Json(ResultInfo.Error("请输入详细地址"));
            }
            if (string.IsNullOrWhiteSpace(model.province))
            {
                return Json(ResultInfo.Error("请选择地址！"));
            }
            string[] DZ = model.province.Split(' ');
            model.province = DZ[0].ToString();
            model.citys = DZ[1].ToString();
            model.areas = DZ[2].ToString();
            if (receiveAdress.Update(model) > 0)
            {
                return Json(ResultInfo.Success("提交成功！"));
            }
            return Json(ResultInfo.Error("操作失败！"));
        }
        #endregion 更新

        #region 删除
        public IActionResult DelAddress(int id)
        {
            ReceiveAddress model = receiveAdress.GetModel(id);
            if (receiveAdress.Delete(model) > 0)
            {
                return Json(ResultInfo.Success("提交成功！"));
            }
            return Json(ResultInfo.Error("操作失败！"));
        }
        #endregion 删除


    }
}